<?php
class MApplication {
    public $title = 'My Mandrill';
    public $defaultLanguage;
    public $modulesFolder = 'modules';
    public $baseUrl = NULL;
    public $routeKeys = array(
        'page'=>'page',
        'redirect'=>'redirect',
    );
    public $routes = array(
        'page'=>array('class'=>'MPage'),
    );

    const SETUP_URL = '?r=ml-setup';
    const ADMIN_MODULE = 'ml-admin';
    const MODULE_PREFIX = 'ml-';
    
    /**
     * @var MDispatcher
     */
    private $_dispatcher;
    
    /**
     * @param String $config
     */
    public function __construct($config = NULL) {
        Mandrill::setApp($this);
        // TODO : Internationalize URL
//        $this->routeKeys = array(
//            'page'=>__t('mandrill.web.getPage'),
//            'redirect'=>__t('mandrill.web.getRedirect')
//        );
//        $this->routes = array(
//            $this->routeKeys['page']=>array('class'=>'MPage'),
//        );
        if (isset($config)) {
            $config = require($config);
            $this->configure($config);
        }

        $this->_dispatcher = MDispatcher::getInstance();

        Mandrill::setApp($this);
    }

    /**
     * @param Array $config
     */
    private function configure($config) {
        if (is_array($config)) {
            foreach ($config as $key=>$value) {
                $this->$key = $value;
            }
        } else {
            throw new MException(MTranslator::translate('mandrill.base.application.configure',NULL,NULL,NULL,'fr'));
        }
    }

    /**
     *
     */
    public function loadModule($module) {
        require_once BASE_PATH.DS.$this->modulesFolder.DS.$module.DS.$module.'.php';
        $class = MNormalizer::normalizeModuleClass($module);
        $moduleClasses = $class::$classes;
        foreach ($moduleClasses as $key=>$value){
            $path = BASE_PATH.DS.$this->modulesFolder.DS.$module.$value;
            $moduleClasses[$key]=$path;
        }
        $result = array_merge(Mandrill::$classes, $moduleClasses);
        Mandrill::$classes = $result;
        MTemplate::getInstance()->setModule($module);
        MPackage::import('mandrill-base-css');
        
        $methods = get_class_methods($class);
        if (!empty($methods)){
            foreach ($methods as $method){
                if (preg_match('#^init#', $method) && !preg_match('#^initAdmin#', $method)){
                    $class::$method();
                }
            }
        }
        
        MTemplate::getInstance()->publishAttachedStylesheets();
        MTemplate::getInstance()->publishAttachedScripts();
    }
    
    public function loadModuleAdmin($module){
        $this->loadModule(MApplication::ADMIN_MODULE);
        require_once BASE_PATH.DS.$this->modulesFolder.DS.$module.DS.$module.'.php';
        $class = MNormalizer::normalizeModuleClass($module);
        $moduleClasses = $class::$adminClasses;
        foreach ($moduleClasses as $key=>$value){
            $path = BASE_PATH.DS.$this->modulesFolder.DS.$module.$value;
            $moduleClasses[$key]=$path;
        }
        $result = array_merge(Mandrill::$classes, $moduleClasses);
        Mandrill::$classes = $result;
        MPackage::import('mandrill-base-css');
        
        $methods = get_class_methods($class);
        if (!empty($methods)){
            foreach ($methods as $method){
                if (preg_match('#^initAdmin#', $method)){
                    $class::$method();
                }
            }
        }
        
        MTemplate::getInstance()->publishAttachedStylesheets();
        MTemplate::getInstance()->publishAttachedScripts();
    }

    /**
     *
     */
    public function run() {
        $this->_dispatcher->dispatch();
    }

}
?>
